<template>
  <BaseMainSlot>
    <template v-slot:searchbox>
      <div class="seach-box">
        <div class="select-box-content">
          <div class="select-box-content-name">任务名称：</div>
          <el-input
            v-model="taskName"
            clearable
            placeholder="请输入任务名称"
            @change="queryPageList()"
          />
        </div>
        <div class="select-box-content">
          <div class="select-box-content-name">执行人：</div>
          <el-select
            clearable
            v-model="personNo"
            placeholder="请选择"
            @change="queryPageList"
          >
            <el-option
              v-for="item in mixUserList"
              :key="item.personNo"
              :label="item.username"
              :value="item.personNo"
            ></el-option>
          </el-select>
        </div>
        <div class="select-box-content">
          <div class="select-box-content-name">生成时间：</div>
          <el-date-picker
            v-model="generateTime"
            placeholder="选择生成时间"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            @change="queryPageList()"
          />
        </div>
      </div>
    </template>
    <template v-slot:btnsBar>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :page-no="154"
        @add="add"
        @del="del"
      />
    </template>
    <template v-slot:content>
      <e-table
        ref="tableData"
        rowKey="taskId"
        :table-data="tableData"
        :table-columns="tableLabel"
        :page="currentPage"
        :size="pageSize"
        :total="totalSize"
        :loading="loading"
        :table-option="tableOption"
        @sizeChange="changePageSize"
        @pageChange="changeCurrentPage"
        @handleSelectionChange="handleSelectionChange"
        @tableUpdatePlus="mixTableUpdatePlus"
      >
        <template slot="equipment" slot-scope="scoped" v-if="scoped.row.taskId">
          <span
            class="table-btns-box"
            @click="mixQueryInspectionPlanTask(scoped.row.taskId)"
          >
            <i class="el-icon-view"></i>
            <span>点击查看</span>
          </span>
        </template>

        <template slot="startTime" slot-scope="scoped" v-if="scoped.row.taskId">
          <span>
            {{ scoped.row.startTime?scoped.row.startTime:'' }}
            <span
              v-if="scoped.row.startTime&&scoped.row.endTime"
            >至</span>
            {{ scoped.row.endTime?scoped.row.endTime:'' }}
          </span>
        </template>
        <template slot="operate" slot-scope="scoped" v-if="scoped.row.taskId">
          <span>
            <span
              class="table-btns-box"
              @click="edit(scoped.row)"
              v-if="judgePermission(154,'edit')"
            >
              <i class="el-icon-edit-outline"></i>
              <span>修改</span>
            </span>
            <span
              class="table-btns-box"
              @click="delOne(scoped.row)"
              v-if="judgePermission(154,'deleteone')"
            >
              <i class="el-icon-delete"></i>
              <span>删除</span>
            </span>
          </span>
        </template>
      </e-table>
      <AddEditDialog
        ref="addEditDialog"
        :now-chance-stau="nowChanceStau"
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showAddEditDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
      <SeeEquip
        :is-show-equip="isShowEquip"
        :equip-data="showCheckList"
        @closeDialog="isShowEquip = false"
      />
    </template>
  </BaseMainSlot>
</template>

<script>
import SeeEquip from "@/components/seeEquip/index.vue";
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import temporaryTaskApi from "@/api/equip-inspection/temporary-task";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new.vue";
// import BaseMainSlot from "@/components/BaseMainSlot/kong.vue";
import AddEditDialog from "./components/temporary-task-add-edit-dialog.vue";
import { mymixin } from "@/utils/mixin";
import { maintenanceMixins } from "@/utils/maintenance-mixins";
import { mapGetters } from "vuex";
import { getToken } from "@/utils/auth";
export default {
  name: "TemporaryTask",
  components: { ActionBar, Pagination, BaseMainSlot, AddEditDialog, SeeEquip },
  mixins: [mymixin, maintenanceMixins],
  data() {
    return {
      generateTime: "",
      equipmentList: [], //设施列表
      // 控制弹出框显示隐藏
      showAddEditDialog: false,
      // 搜索条件:点检任务名称：
      taskName: "",
      pastList: [
        { name: "已过期", id: true },
        { name: "未过期", id: false },
      ],
      checkedIdList: [],
      loading: false,
      // 表单展示项
      tableLabel: [
        {
          label: "任务编号",
          param: "taskCode",
          show: true,
          notSearch: true,
        },
        {
          label: "任务名称",
          param: "taskName",
          show: true,
        },
        {
          label: "关联设备",
          param: "equipment",
          show: true,
          notSearch: true,
          slotName: "equipment",
          width: 120,
        },
        {
          label: "任务生成时间",
          param: "generateTime", // 表格显示参数
          show: true,
          type: "datetime",
        },
        {
          label: "执行人",
          param: "userName",
          show: true,
          width: 200,
          notSearch: true,
        },
        {
          label: "任务执行时间",
          param: "startTime",
          type: "datetime_range",
          slotName: "startTime",
          show: true,
          width: 400,
          notSearch: true,
        },
      ],

      // 表单事件
      tableOption: {
        label: "操作",
        options: [
          {
            label: "操作",
            type: "text",
            icon: "el-icon-edit-outline",
            methods: "operate",
            slotName: "operate",
          },
        ],
      },
      //  搜索条件:设备名称：
      equipmentName: "",
      equipmentArr: [],
      //  搜索条件:执行人
      personNo: "",
      startTime: [],
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission",
    }),
  },
  watch: {},
  created() {
    this.queryPageList();
    this.mixQueryUserList();
    this.mixQueryInspectionHaveCheckList();
  },
  mounted() {},
  methods: {
    // 查看设施列表
    lookEquipmentList(row) {
      this.equipmentList = row.facilityList;
      this.isShowEquip = true;
    },

    querySearch(queryString, cd) {
      var equipmentData = this.equipmentArr;
      let results = queryString
        ? equipmentData.filter(this.createFilter(queryString))
        : equipmentData;
      // 调用 callback 返回建议列表的数据
      cd(results);
    },
    createFilter(queryString) {
      return (equipmentData) => {
        return (
          equipmentData.value
            .toLowerCase()
            .indexOf(queryString.toLowerCase()) === 0
        );
      };
    },
    queryPageList(e) {
      this.loading = false;
      // 获取数据
      var params = {
        page: this.currentPage,
        size: this.pageSize,
      };
      var condition = [
        { paramName: "whetherTemporary", value: true, sign: "equals" },
        {
          paramName: "orgainzationMark",
          value: getToken("organizationNo"),
          sign: "equals",
        },
      ];
      if (this.taskName || e?.taskName) {
        condition.push({
          paramName: "taskName",
          value: this.taskName || e?.taskName,
          sign: "like",
        });
      }
      if (this.equipmentName || e?.equipmentName) {
        condition.push({
          paramName: "equipmentName",
          value: this.equipmentName || e?.equipmentName,
          sign: "like",
        });
      }
      if (this.personNo || e?.personNo) {
        condition.push({
          paramName: "personNo",
          value: this.personNo || e?.personNo,
          sign: "equals",
        });
      }
      if (this.generateTime || e?.generateTime) {
        condition.push({
          paramName: "generateTime",
          value: this.generateTime,
          sign: "gte",
        });
      }
      if (sessionStorage.farming) {
        condition.push({
          paramName: "farming",
          value: sessionStorage.farming,
          sign: "equals",
        });
      }
      if (condition.length > 0) {
        params["condition"] = encodeURIComponent(JSON.stringify(condition));
      }
      temporaryTaskApi.queryPageList(params).then((res) => {
        this.loading = false;
        if (res.code === 1) {
          let data = res.data;
          this.totalSize = data.total;
          this.tableData = data.list;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    handleSelectionChange(list) {
      if (list.length == 0) {
        this.delActive = false;
        return;
      }
      this.checkedIdList = [];
      let emptyList = [];
      list.map((item) => {
        if (item.taskId) {
          this.checkedIdList.push(item.taskId);
        } else {
          emptyList.push(item);
        }
      });
      if (this.checkedIdList.length >= 1) {
        this.delActive = true;
      } else {
        this.delActive = false;
        this.$message.warning("不可选择空数据！");
      }
      if (list.length === emptyList.length) {
        this.toggleNoSelection();
      } else {
        this.toggleNoSelection(emptyList);
      }
    },
    // 单个删除
    delOne(data) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass",
      })
        .then(() => {
          temporaryTaskApi
            .delList({ taskIdList: [data.taskId] })
            .then((res) => {
              if (res.code === 1) {
                this.$message.success(res.msg);
                this.queryPageList();
                this.resetActionBar();
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 表格删除
    delmessage() {
      temporaryTaskApi
        .delList({ taskIdList: this.checkedIdList })
        .then((res) => {
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.queryPageList();
            this.resetActionBar();
          }
        });
    },
    // 添加
    add() {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    edit(row) {
      if (row.taskId) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.seach-box {
  display: flex;
  justify-content: flex-start;
  .select-box-content {
    width: fit-content;
  }
}
</style>
